package io.tec.cloud.algorithm.c03_v20220607;

public class Code04_RingArray {

    static class MyQueue<E> {
        int[] arr;
        int size;
        int limit;
        int pushi;
        int popi;

        public MyQueue(int limit) {
            arr = new int[limit];
            this.limit = limit;
        }

        public int addToPushI(int e) {
            if (size >= limit) {
                throw new RuntimeException("满了");
            }
            arr[pushi] = e;
            size++;
            pushi = reIndex(pushi);
            return e;
        }


        public int removePopI() {
            if (size <= 0) {
                throw new RuntimeException("空了");
            }
            int e = arr[popi];
            size--;
            popi = reIndex(popi);
            return e;
        }


        private int reIndex(int index) {
            return index < limit - 1 ? index + 1 : 0;
        }
    }
}
